<%@page language="java" contentType="text/html; charset=utf-8" pageEncoding="utf-8"%>
<%@include file="/common/session.jsp"%>
<c:set var="contractId" value="${param.contractId}"/>
<c:set var="detail" value="${param.detail}"/>
<!DOCTYPE html>
<html>
<head>
    <meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
    <meta name="viewport" content="initial-scale=1.0, minimum-scale=1.0, maximum-scale=2.0, user-scalable=no, width=device-width">
    <LINK type="text/css" rel="stylesheet" href="${ctx}/css/default.css"/>
    <LINK type="text/css" rel="stylesheet" href="${ctx}/js/layer/theme/default/layer.css"/>
    <script type="text/javascript" src="https://cdn.bootcss.com/jquery/1.9.1/jquery.min.js"></script>
    <script type="text/javascript" src="${ctx}/js/xtable.js"></script>
    <script type="text/javascript" src="${ctx}/dwr/engine.js"></script>
    <script type="text/javascript" src="${ctx}/dwr/util.js"></script>
    <script type='text/javascript' src='${ctx}/dwr/interface/RegionRpcService.js'></script>
    <script type='text/javascript' src='${ctx}/dwr/interface/ContractRpcService.js'></script>
    <script type='text/javascript' src='${ctx}/dwr/interface/ContractRegionRpcService.js'></script>
    <script type="text/javascript" src="${ctx}/js/loading.js"></script>
    <script type="text/javascript" src="${ctx}/js/layer/layer.js"></script>
</head>
<body>
<p class="contact-input" style="text-align: right">
	<input type="hidden" id="contractId" name="contractId" value="${contractId}">
    <c:if test="${detail != '1'}">
        <button type="button" onclick="submitContractRegion('${contractId}','')">添加合同明细</button>
    </c:if>
    <button type="button" onclick="callbackContract()">返回合同列表</button>
</p>

<form id="contractRegionForm"  name ="contractRegionForm" style="display: none;">
    <input type="hidden" id="contractRegionState" name="contractRegionState" value="1">
    <table width="100%" cellpadding="5" cellspacing="5">
        <tr>
            <td>
                选择区域:
            </td>
            <td>
                <select id="regionId" name="regionId" onchange="getRegionInfo()">
                </select>
            </td>
            <td id="label-title-area">
                区域总面积:
            </td>
            <td>
                <input type="number" id="regionNum"  name="regionNum" readonly="true"/>
            </td>
        </tr>
        <tr>
            <td id="label-title-Num">
                区域剩余面积:
            </td>
            <td>
                <input type="number" id="regionRestNum"  name="regionRestNum" readonly="true"/>
            </td>
            <td id="label-ren-fee">
                月平米租金:
            </td>
            <td>
                <input type="number" id="contractRegionRentFee" name="contractRegionRentFee" readonly="true"/>
            </td>
            <td id="label-rent-area">
                租用面积:
            </td>
            <td>
                <input type="number"  placeholder="租用面积/车位(必填)" id="contractRegionRentCount" name="contractRegionRentCount" onchange="countAmount()" maxlength="7" required/>
            </td>
        </tr>
        <tr>
            <td>
                月租金：
            </td>
            <td>
                <input type="number" id="contractRegionAmount" name="contractRegionAmount" readonly="true"/>
            </td>
            <td>
                场地编号：
            </td>
            <td>
                <input type="text" placeholder="场地编号(必填)"  id="contractRegionCarNo" name="contractRegionCarNo" maxlength="15" required/>
            </td>
            <td>
                车位数：
            </td>
            <td>
                <input type="number" id="contractRegionParkCount" name="contractRegionParkCount"  maxlength="10"/>
            </td>
        </tr>
    </table>

</form>

<SCRIPT LANGUAGE="JavaScript" >
    var contractThead = new Array(
        new XThead("区域名称"),
        new XThead("租金(元)"),
        new XThead("租赁面积/车位"),
        new XThead("租赁总金额(元)"),
        new XThead("合同起始日期"),
        new XThead("合同截止日期"),
        new XThead("状态"),
        <c:if test="${detail != '1'}">
        new XThead("操作")
        </c:if>
    );

    var contractXTable = new XTable("contract", contractThead, true);

    //    是否有效
    var state_hash = {
        "0": "无效",
        "1": "有效"
    }

    var  type_unit = {
        "1": '平米',
        "2": '个'
    }
    contractXTable.XTbodyArray = function (model) {
        var array = new Array(
            new XTbody("contractRegion", model.region.regionName, "string", null, null, "center"),
            new XTbody("contractRegion", model.contractRegionRentFee, "string", null, null, "center"),
            new XTbody("contractRegion", model.contractRegionRentCount + type_unit[model.region.regionType],"string", null, null, "center"),
            new XTbody("contractRegion", model.contractRegionAmount, "string", null, null, "center"),
            new XTbody("contractRegion", model.contract.contractStartDate.Format("yyyy-MM-dd"), "string", null, null, "center"),
            new XTbody("contractRegion", model.contract.contractEndingDate.Format("yyyy-MM-dd"), "string", null, null, "center"),
            new XTbody("contractRegion", state_hash[model.contractRegionState], "string", null, null, "center"),
            <c:if test="${detail != '1'}">
            new XTbody("contractRegion", model.contractRegionState ==1  ? "<a href='#'  style='border-right: 1px solid #dddddd' class='contact-input'  onclick=submitContractRegion('"+ model.contractId +"','"+ model.contractRegionId + "')>编辑&nbsp;</a>"+
                "<a href='#'  class='contact-input'  onclick=changeState('"+ model.contractRegionId +"','"+ model.region.regionNum +"','"+ model.region.regionId +"',0)>&nbsp;中止</a>":"<a href='#'  class='contact-input'  onclick=changeState('"+ model.contractRegionId +"','"+ model.region.regionNum +"','"+ model.region.regionId +"',1)>&nbsp;续签</a>", "string", null, null, "center")
            </c:if>
        );
        return array;
    }


    function getContractPage() {
        var contractRegion = new ContractRegion();
        contractRegion.contractId = dwr.util.getValue("contractId");
        ContractRegionRpcService.selectContractRegionList(contractRegion, function (list) {
            contractXTable.addTable(list);
        });
    }
</SCRIPT>
</body>
</html>
<script>

    //    显示区域列表
    var region = new Region();
    region.regionState = "1";
    region.marketId = '${marketId}';
    RegionRpcService.selectRegionList(region, callbackSelectRegionList);
    function callbackSelectRegionList(list) {
        dwr.util.addOptions("regionId", list, "regionId", "regionName");
    }

    //    获取区域内容
    function getRegionInfo(){
        var region = new Region();
        region.regionId = dwr.util.getValue("regionId");
        RegionRpcService.selectRegionList(region, callbackSelectRegionInfo);
    }

    function callbackSelectRegionInfo(list){
        if(list[0].regionType == 1){
            dwr.util.setValue("label-title-area","区域总面积:");
            dwr.util.setValue("label-title-Num","区域剩余面积");
            dwr.util.setValue("label-ren-fee","月平米租金");
            dwr.util.setValue("label-rent-area","租用面积");
        }
        else if(list[0].regionType == 2){
            dwr.util.setValue("label-title-area","区域车位数:");
            dwr.util.setValue("label-title-Num","区域剩余车位");
            dwr.util.setValue("label-ren-fee","月车位租金");
            dwr.util.setValue("label-rent-area","租用车位");
        }
        dwr.util.setValue("regionNum", list[0].regionNum);
        dwr.util.setValue("regionRestNum", list[0].regionRestNum);
        dwr.util.setValue("contractRegionRentFee", list[0].regionRentFee);
    }

    function countAmount(){
        var count = dwr.util.getValue("contractRegionRentCount");
        var fee = dwr.util.getValue("contractRegionRentFee");
        var contractRegionAmount = count * fee;
        dwr.util.setValue("contractRegionAmount",contractRegionAmount);
    }


    getContractPage();

    function submitContractRegion(contractId,contractRegionId){
        layer.open({
            type:1,
            title:"管理合同区域",
            content: $("#contractRegionForm"),
            btn:['确定','取消'],
            area: ['893px', '300px'],
            yes: function(){
                insertOrUpdateContractRegion(contractId,contractRegionId);
            },btn2: function(){
            }
        })
        if (contractRegionId.length > 0){
            getRegionFormInfo(contractRegionId);
        }else{
            intContractRegionForm();
        }

    }

    function intContractRegionForm(){
        contractRegionForm.reset();
        getRegionInfo();
    }

    function insertOrUpdateContractRegion(contractId,contractRegionId){
        if (!validate(contractRegionForm)) {
            return false
        }
        if (parseInt(dwr.util.getValue('contractRegionRentCount')) > parseInt(dwr.util.getValue('regionRestNum'))){
            alert('添加面积(车位)不能超过剩余面积(车位)')
            dwr.util.setValue('contractRegionRentCount','')
            return false
        }
        var contractRegion = dwr.util.getValues("contractRegionForm");
        contractRegion.contractId = contractId;
        contractRegion.regionId = dwr.util.getValue("regionId");
        contractRegion.region = new Region();
        contractRegion.region.regionNum = dwr.util.getValue("regionNum");
        contractRegion.region.regionId = dwr.util.getValue("regionId");
        if (contractRegionId.length > 0) {
            contractRegion.contractRegionId = contractRegionId;
            ContractRegionRpcService.updateContractRegion(contractRegion, callbackContractRegion());
        }else{
            ContractRegionRpcService.insertContractRegion(contractRegion, callbackContractRegion());
        }

    }


    function callbackContractRegion(){
        layer.closeAll();
        layer.msg('添加成功');
        window.location.reload();
    }

    function  getRegionFormInfo(contractRegionId){
        var contractRegion = new ContractRegion();
        contractRegion.contractRegionId = contractRegionId;
        ContractRegionRpcService.selectContractRegionList(contractRegion, callbackSelectContractRegionList);
        function callbackSelectContractRegionList(list) {
            dwr.util.setValues(list[0]);
            getRegionInfo();
        }
    }

    function  changeState(contractRegionId,num,regionId,state){
        if(confirm('确定吗？')){
            var contractRegion = new ContractRegion();
            contractRegion.contractRegionId = contractRegionId;
            contractRegion.contractRegionState = state;
            contractRegion.regionId = regionId;
            contractRegion.region = new Region();
            contractRegion.region.regionNum = num;
            contractRegion.region.regionId = regionId;
            ContractRegionRpcService.updateContractRegion(contractRegion, callbackUpdateContractRegion);
        }else{
            return;
        }
        function callbackUpdateContractRegion() {
            if (state==1){
                alert('续签成功!');
            }
            else{
                alert('终止成功!');
            }
            location.reload();
        }
    }


    function callbackContract(){
        <%--location = "${ctx}/contract/contract_list.jsp";--%>
        window.history.back();
    }


</script>
